%include {
#include "calc.h"
#include <stdlib.h>
#include "assert.h"
}

%token_type {int}
//priority and terminal
%left PLUS MINUS.
%left DIVIDE TIMES.


//when error happens
%syntax_error{
puts("system error");
exit(1);
}

//the program is a expr (no terminal)
program ::=expr(A). {
//if match this patten, print the output
printf("res:%d",A);
}

//文法定义
expr(A) ::=expr(B) PLUS expr(C). {A=B+C;}
expr(A) ::=expr(B) MINUS expr(C). {A=B-C;}
expr(A) ::=expr(B) TIMES expr(C). {A=B*C;}
expr(A) ::=expr(B) DIVIDE expr(C). {
 if(C==0){
 puts("divid by o");
 }else{
 	A=B/C;
 }
expr(A) ::=INTEGER(B). {A=B;}


%code{
int main(){
	void* pParser=ParseAlloc(malloc);
	Parse(pParser,INTEGER,1);
	Parse(pParser,DIVIDE,0);
	Parse(pParser,INTEGER,1);
	Parse(pParser,0,0);
	ParseFree(pParser,free);
}

}